{extend name="public/base"}

{block name="title"}
我的采购
{/block}

{block name="style"}
{css href="/static/wechat/css/order.css" /}
<style>
    .weui-form-preview__label {
        text-align: left;
        font-size: .7rem;
        max-width: 70%;
    }
</style>
{/block}

{block name="body"}
<div class="pageControl orderList">
    <div class="msui_nav msui_navTop">
        <a class="msui_navTop-back" href="javascript:history.back();" _href="{:url('r_member/deal_index')}"><i class="micon-back"></i></a>
        <h1 class="msui_navTop-title">我的采购</h1>
    </div>
    <div class="msui_menu">
        <div class="msui_menu-mask" style="display: none;">
            <ul class="msui_menu-bd js-menu" style="width:auto;">
                <li class="msui_menu-li" dataTab="1" style="width:48%;"><a style="width:100%;text-align:center;" class="msui_tabMenu-active msui_menu-item fs08 cl-9999" href="?status=0" data-status="0">我销售的</a>
                <li class="msui_menu-li" dataTab="2" style="width:48%;"><a style="width:100%;text-align:center;" class="msui_menu-item fs08 cl-9999" href="?status=1" data-status="1">我采购的</a></li>
                <!-- 用于动画的层 -->
                <div id="line" class="msui_menu-baseline" style="width:50%;left:0;"></div>
            </ul>
        </div>
        <!--<div class="msui_menu-dropdown">-->
            <!--<i class="micon-down"></i>-->
        <!--</div>-->
    </div>
    <div class="mtab_list">
        <div class="mtab_list-bd">

        </div>
    </div>
</div>


<!--<div style="position:fixed;left:0;bottom:0;z-index:9;width:100%;height:2rem;line-height:2rem;background:#FFAD2C;text-align:center;">-->
    <!--<a href="{:url('r_member/deal_index')}" style="color:#FFF;float:left;width:49%;" onclick="deal_push_old_but.click();">销售</a>-->
    <!--<a href="{:url('r_orders/deal_order')}" style="color:#FFF;float:right;width:49%;">订单</a>-->
<!--</div>-->


<!-- loading toast -->
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast" style="opacity: 1; visibility: visible;">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">加载中...</p>
    </div>
</div>
{/block}
{block name="javascript"}
<script>
    $(function () {

        // 确认完成
        $(document).off('click', '.affirm_finish');
        $(document).on('click', '.affirm_finish', function () {
            if (!confirm('确认完成订单！！')) {
                return false;
            }
            $('#loadingToast').fadeIn();
            //点击确认
            var _this = $(this);
            var id = _this.data('order-id');
            var model = _this.data('model');
            $.get("/wechat/orders/affirm_finish", {id : id, model : model}, function(msg) {
                $('#loadingToast').fadeOut();
                if (msg.code == 200) {
                    $.alert('操作成功！');
                    if (_this.hasClass("order_list")) {
                        _this.parents('.status').find("span").text('订单完成');
                    } else {
                        $(".action").text('订单完成');
                    }
                    var _type = model == 'order_server' ? 2 : 1;
                    _this.text('待评价').attr('href', "{:url('w_comment/index')}?order_id=" + id + "&type=5").removeClass('affirm_finish');
                } else {
                    $.alert(error_code[msg.code]);
                }
            }, 'json');
        });


        var obj = {
            'is_more' : true,
            'page' : 1,
            'url' : '{:url("get_order_server_list_deal")}',
            'status' : {$status}
        };

        if (obj.status > 0) {
            // 默认定位到进来的状态
            var data_status_index = $("a[data-status='"+obj.status+"']").parent().index() + 1;
            var _li_width = $('.msui_menu-bd').find('.msui_menu-li').width();
            var _w_width = $(window).width();
            if (_w_width - data_status_index * _li_width < _li_width) {
                if (_w_width < 372) {
                    $(".msui_menu-mask").scrollLeft((data_status_index - 4) * _li_width);
                } else {
                    $(".msui_menu-mask").scrollLeft((data_status_index - 5) * _li_width);
                }
            }
            $(".msui_menu-mask").find('a').removeClass('msui_tabMenu-active');
            $("a[data-status='"+obj.status+"']").addClass('msui_tabMenu-active');
            $("#line").animate({
                marginLeft:(data_status_index - 1) * _li_width
            }, 300);
        }

        get_order_list();
        $(window).scroll( function() {
            if (($(document).height() - $(this).scrollTop() - $(this).height()<50) && obj.is_more){
                get_order_list();
            }
        });
//        $(".js-menu a").click(function (e) {
//            e.preventDefault();
//            obj.page = 1;
//            obj.status = $(this).data('status');
//            $(".mtab_list-bd").html('');
//            get_order_list();
//        });

        function get_order_list() {
            $('#loadingToast').fadeIn(100);
            obj.is_more = false;
            $.ajax({
                type : 'POST',
                url : obj.url + '?page=' + obj.page,
                data : {
                    status : obj.status
                },
                dataType : 'json',
                success : function (msg) {
                    $('#loadingToast').fadeOut(100);
                    if (obj.page == 1) {
                        $(".mtab_list-bd").html(msg.tpl);
                    } else {
                        $(".mtab_list-bd").append(msg.tpl);
                    }
                    // 计算是否还有更多数据
                    if ((msg.data.total - (msg.data.current_page * msg.data.per_page)) > 0) {
                        obj.is_more = true;
                    }
                    obj.page++;
                }
            });
        }
    });

</script>
{/block}